//
//  QCEmotionPageViewCell.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/2/11.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit

class QCEmotionPageViewCell: UICollectionViewCell {
    
    var indexPath: IndexPath?{
        didSet{
            
        }
    }
    
    var emotions:[QCEmotionModel]?{
        didSet{
            //把20个按钮隐藏
            for button in emotionButtonArray {
                button.isHidden = true
            }
            for (i,emotionModel) in emotions!.enumerated(){
             let button = emotionButtonArray[i]
               //赋值
                button.emotionModel = emotionModel
                //赋值之后显示button
                button.isHidden = false
                
                if emotionModel.isEmoji {
                    let code = ((emotionModel.code ?? "") as NSString).emoji()
                    //设置title
                    button.setTitle(code, for: UIControlState.normal)
                    button.setImage(nil, for: UIControlState.normal)
                }else {
                    
                    let path = emotionModel.path ?? ""
                    //获取bundle中 图片
                    let image = UIImage(named: path, in: QCEmotionTools.shared.emotionBundle, compatibleWith: nil)
                    //图片表情
                    button.setImage(image, for: UIControlState.normal)
                    button.setTitle(nil, for: UIControlState.normal)
                    
                }
                
            }
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //定义一个数组用来保存按钮 
    var emotionButtonArray:[QCEmotionButton] = [QCEmotionButton]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  override  func layoutSubviews() {
        super.layoutSubviews()
      let buttonW = (KSCREENW - 10) / CGFloat(QCEMOTIONMAXCOL)
      let buttonH = (216 - 35 - 20) / CGFloat(QCEMOTIONMAXROW)
    
    for (i,button) in emotionButtonArray.enumerated() {
        
        // 列索引
        let colIndex = CGFloat(i%QCEMOTIONMAXCOL)
        // 行索引
        let rowIndex = CGFloat(i/QCEMOTIONMAXCOL)
        // 设置frame
        button.frame = CGRect(x: 5 + colIndex*buttonW, y: rowIndex*buttonH, width: buttonW, height: buttonH)
    }
    deleteButton.frame = CGRect(x: KSCREENW - buttonW - 5, y: buttonH*2, width: buttonW, height: buttonH)
    
    }
    
    
    private func setupUI(){
        
        addChildButton()
        contentView.addSubview(deleteButton)
       
    }
    
    private func addChildButton(){
        
        for _ in 0..<QCEMOTIONMAXCOUNT {
            
           let button = QCEmotionButton()
            
            button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
            
            button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
            emotionButtonArray.append(button)
            contentView.addSubview(button)
        }
        
    }
    
    private lazy var deleteButton: UIButton = {
       
        let button = UIButton()
        
        button.addTarget(self, action: #selector(deleteButtonClick), for: UIControlEvents.touchUpInside)
        button.setImage(UIImage(named:"compose_emotion_delete"), for: UIControlState.normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), for: UIControlState.highlighted)
        
        
        return button
    }()
    
    @objc private func buttonClick(button:QCEmotionButton){
        
        //按钮点击发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue:EMOTIONBUTTONNOTI), object:button.emotionModel)
        
    }
    
    @objc private func deleteButtonClick(){
       //点击删除按钮发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue:EMOTIONDELETEBUTTONNOTI), object: nil)
        
    }
    
}
